package com.example.market2023.controller;


import com.example.market2023.entity.Goods;
import com.example.market2023.service.GoodsService;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    HttpServletRequest request;
    //    1.展示数据库里已有的商品列表给前端
    @RequestMapping("/goodsList")
    public String goodsList(Model model){
        Object username= request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else {
//        1.调用service方法查询到商品列表结果
            List<Goods> goodsList = goodsService.findAll();
            System.out.println(goodsList);

//        2.将查询结果用model绑定
            model.addAttribute("data", goodsList);
            model.addAttribute("username", username);
//        3.return 视图
            return "goods/goodsList";
        }
    }
    //    2.前端实现增加、删除、更改操作这里进行处理
    @RequestMapping("/goodsAdd")
    public String goodsAdd(Model model){
        Object username= request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else {
            model.addAttribute("username", username);
            return "goods/goodsAdd";
        }
    }

    /**
     * 新增商品处理
     * @param name
     * @param price
     * @param quantity
     * @param category
     * @param supplier
     * @return
     */
    @RequestMapping("/goodsAddProcess")
    public String goodsAddProcess(@RequestParam String name,@RequestParam String price,
                                  @RequestParam String quantity,@RequestParam String category,
                                  @RequestParam String supplier){
//        1.组装成goods实体
        Goods goods=new Goods(null,name,Float.valueOf(price),Integer.valueOf(quantity),category,supplier,"0");
//       2.调用service的add方法增加
        if(goodsService.add(goods)==1){
            return "redirect:/goods/goodsList";
        }else{
            return "redirect:/goods/goodsList";
        }
    }

    //   处理更新商品,首先要给出一个页面显示该条记录的数据
    @RequestMapping("/goodsUpdate")
    public String update(@RequestParam String id,Model model){
//        首先基于id查询出来该id记录里的所有数据信息
        Goods goods=new Goods();
        goods.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
        Goods goodDB = goodsService.findOne(goods);
//        2.将这些数据信息返回给视图
        model.addAttribute("data",goodDB);
        Object username = request.getSession().getAttribute("username");
        model.addAttribute("username",username);
//        3.返回视图文件
        return "goods/goodsUpdate";
    }

//    @RequestMapping("/goodsUpdateProcess")
//    public String goodsUpdateProcess(@RequestParam String name,@RequestParam String price
//            ,@RequestParam String quantity,@RequestParam String category,
//                                     @RequestParam String supplier){
//        //      组装一个goods实体对象
//        Goods  goods = new Goods();
//        goods.setName(name);
//        goods.setPrice(Float.valueOf(price));
//        goods.setQuantity(Integer.valueOf(quantity));
//        goods.setCategory(category);
//        goods.setSupplier(supplier);
//        //        调用service的add方法给数据库
//        if (goodsService.add(goods)==1){
//            //        如果返回ok，那么就进入商品管理首页
//            return "redirect:/goods";
//        }else {
//            return "redirect:/goods";
//        }
//    }

}


